package com.beck.security.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {
    @RequestMapping("/login")
    public String toLogin() {
        System.out.println("in to login....");
        // 应用程序中获得用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        System.out.println("authentication:"+authentication.getName()+","+authentication.getDetails());
        System.out.println("principal:"+authentication.getPrincipal());
        return "login";
    }

    @RequestMapping("/success")
    public String success() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        System.out.println("authentication:"+authentication);
        System.out.println("principal:"+authentication.getPrincipal());
        System.out.println("in success....");
        return "success";
    }

    @RequestMapping("/main")
    public String main() {
        System.out.println("in main()...");
        return "/main";
    }
    @RequestMapping("/myerror")
    public String error() {
        System.out.println("in main()...");
        return "/error";
    }

    @RequestMapping("/test")
    @ResponseBody
    public String test(){
        System.out.println("in test....");
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        User user = (User) authentication.getPrincipal();
        System.out.println(user);
        return "success";
    }

}
